package com.magus.project.yddemo.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.magus.framework.core.controller.BaseController;
import com.magus.framework.core.entity.BaseTreeNode;
import com.magus.framework.core.entity.BaseTreeNode.TreeWithCheckedView;
import com.magus.framework.core.entity.BaseTreeNode.TreeWithOutCheckedView;
import com.magus.framework.core.exception.app.ControllerException;
import com.magus.framework.core.webbean.TreeResult;
import com.magus.project.yddemo.entity.IndicatorGroup;
import com.magus.project.yddemo.service.IndicatorGroupService;
import com.magus.project.yddemo.service.IndicatorService;
import com.magus.project.yddemo.vobean.IndicatorVoBean;

import springfox.documentation.annotations.ApiIgnore;

/**
 * 指标组树
 * 
 * @author xujuan
 * @date 2018-01-19 15:26:53
 */
@ApiIgnore
@RestController
@RequestMapping("/yddemo/indicatorGroupTree")
public class IndicatorGroupTreeController extends BaseController {
	@Autowired
	private IndicatorGroupService indicatorGroupService;
	@Autowired
	private IndicatorService indicatorService;

	@GetMapping
	@JsonView(TreeWithOutCheckedView.class)
	ResponseEntity<TreeResult> findTree(@RequestParam("node") String node) {
		String[] node_param = StringUtils.split(node, "_");
		if (node_param.length != 2) {
			throw new ControllerException("请求参数出错：" + node);
		}

		String nodeType = node_param[0];
		// String nodeId = node_param[1];

		TreeResult result = new TreeResult();
		nodeType = nodeType.toLowerCase().trim();

		if ("root".equals(nodeType)) {
			result.setChildren(findRootChilren());
		}

		return ResponseEntity.ok(result);
	}

	@GetMapping("/getAllIndicator")
	@JsonView(TreeWithCheckedView.class)
	ResponseEntity<TreeResult> findAllTree(@RequestParam("node") String node, String dimension) {
		String[] node_param = StringUtils.split(node, "_");
		if (node_param.length != 2) {
			throw new ControllerException("请求参数出错：" + node);
		}

		String nodeType = node_param[0];
		String nodeId = node_param[1];

		TreeResult result = new TreeResult();
		nodeType = nodeType.toLowerCase().trim();

		if ("root".equals(nodeType)) {
			result.setChildren(findRootChilren1(nodeId, node, dimension));
		} else if ("indicatorgroup".equals(nodeType)) {
			result.setChildren(findOtherChilren(nodeId, node, dimension));
		}

		return ResponseEntity.ok(result);
	}

	/**
	 * 获取ROOT子节点
	 */
	private List<BaseTreeNode> findRootChilren() {
		List<BaseTreeNode> nodes = new ArrayList<>();

		List<IndicatorGroup> groups = indicatorGroupService.findAll();
		for (IndicatorGroup group : groups) {
			group.setNodePid("root_root");
			group.setChecked(null);
			group.setExpanded(false);
			group.setLeaf(true);
			nodes.add(group);
		}

		return nodes;
	}

	private List<BaseTreeNode> findRootChilren1(String nodeId, String node, String dimension) {
		List<BaseTreeNode> nodes = new ArrayList<>();

		List<IndicatorGroup> groups = indicatorGroupService.findAll();
		for (IndicatorGroup group : groups) {
			group.setNodePid("root_root");
			group.setChecked(null);
			group.setExpanded(true);
			group.setLeaf(false);
			group.setChildren(findOtherChilren(group.getId(),group.getNodeId(),dimension));
			nodes.add(group);
		}

		return nodes;
	}

	/**
	 * 获取指标包的子节点
	 */
	private List<BaseTreeNode> findOtherChilren(String nodeId, String node, String dimension) {
		List<BaseTreeNode> nodes = new ArrayList<>();

		List<IndicatorVoBean> indicatorVoBeans = indicatorService
				.findIndicatorVoBeanByGrpup(indicatorGroupService.findOne(nodeId), dimension);
		for (IndicatorVoBean group : indicatorVoBeans) {
			group.setNodePid(node);
			group.setChecked(false);
			group.setExpanded(false);
			group.setLeaf(true);
			group.setText(group.getName());
			nodes.add(group);
		}

		return nodes;
	}

}
